package com.yuanzi.base.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.yuanzi.base.util.StringUtils;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * jedis连接池
 * @author chengshx
 * @date 2017年5月5日
 */
@Configuration
public class RedisPool {

	@Bean
	@Autowired
	public JedisPool jedisPool(@Qualifier("redis.pool.config") JedisPoolConfig config,
			@Value("${spring.redis.host}") String host, @Value("${spring.redis.port}") int port,
			@Value("${spring.redis.timeout}") int timeout, @Value("${spring.redis.password}") String password) {
		if(StringUtils.isEmpty(password)) {
			return new JedisPool(config, host, port, timeout);
		} else {
			return new JedisPool(config, host, port, timeout, password);
		}
	}
	
	@Bean(name="redis.pool.config")
	@ConfigurationProperties(prefix="spring.redis.pool")
	public JedisPoolConfig jedisPoolConfig() {
		JedisPoolConfig config = new JedisPoolConfig();
		return config;
	}
}
